package com.zaofan.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Setting;
import com.zaofan.bean.Version;
import com.zaofan.cache.FrontCache;
import com.zaofan.service.SettingService;
import com.zaofan.service.VersionService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/system")
public class SystemController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private SettingService settingService = new SettingService();
	private VersionService versionService = new VersionService();

	protected void updateSetting(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Setting setting = WebUtils.write2bean(request, Setting.class);
			setting.setId(1);
			settingService.update(setting);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	protected void updateVersion(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Version version = WebUtils.write2bean(request, Version.class);
			version.setId(1);
			versionService.update(version);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}
	
	protected void updateDate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			FrontCache cache = FrontCache.getInstance();
			cache.loadAllCache();
			JsonUtils.writeSuccessJson("更新成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("更新失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("更新失败!!", response);
	}
}
